package com.gxc.authcenter.server.util;

import org.apache.log4j.Logger;
import org.springframework.remoting.rmi.RmiServiceExporter;

import com.gxc.authcore.system.cache.service.AuthCacheService;
import com.gxc.authcore.system.cache.service.AuthCacheServiceImpl;
import com.gxc.authcore.system.util.ParamUtil;

public class RmiServiceUtil {

	private static Logger logger = Logger.getLogger(RmiServiceUtil.class);
	private static RmiServiceExporter authServiceExporter;

	/**
	 * 开启认证缓存服务
	 */
	public static void startAuthService() {
		if (authServiceExporter == null) {
			authServiceExporter = new RmiServiceExporter();
		}
		try {
			String rmi_url = ParamUtil.get("rmi_authCacheService");
			String serviceName = rmi_url
					.substring(rmi_url.lastIndexOf("/") + 1);
			String rmi_port = rmi_url.substring(rmi_url.lastIndexOf(":") + 1);
			rmi_port = rmi_port.substring(0, rmi_port.indexOf("/"));
			AuthCacheService authCacheService = new AuthCacheServiceImpl();
			authServiceExporter.setService(authCacheService);
			authServiceExporter.setServiceName(serviceName);
			authServiceExporter.setServiceInterface(AuthCacheService.class);
			authServiceExporter.setRegistryPort(Integer.valueOf(rmi_port));
			authServiceExporter.afterPropertiesSet();
			logger.info("开启认证服务");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
